function setCookie(name, value, exptime, domain){ 
    var domain = domain ? domain : "mse.sogou.com";
    var exp  = new Date();
    exp.setTime(exp.getTime() + exptime);
    document.cookie = name + "=" + value + ";path=/;expires=" + exp.toGMTString() + ";domain=" + domain + ";";
}
function getCookie1(offset) { 
    var endstr = document.cookie.indexOf (";", offset);
    if (endstr == -1) endstr = document.cookie.length;
    return unescape(document.cookie.substring(offset, endstr));
}
function getCookie2(name) { 
    var arg = name + "=";
    var alen = arg.length;
    var clen = document.cookie.length;
    var i = 0;
    while (i < clen) {
        var j = i + alen;
        if (document.cookie.substring(i, j) == arg) return getCookie1(j);
        i = document.cookie.indexOf(" ", i) + 1; 
        if (i == 0) break; 
    } 
    return null; 
}
var PingbackApp = function(t){
    //参数t为设备类型
    var solution = t ? t : "";//方案
    var pingServerUrl = "http://ping.mse.sogou.com/";
    var n = new Date().getTime();
    var c = escape(n*1000+Math.round(Math.random()*1000));
    this.getUid = function(){
        var uid = "";
        if(getCookie2("SMYUV") != null) {
            uid = getCookie2("SMYUV");
        } else {
            uid = c;
            setCookie("SMYUV", uid, 2592000000, "sogou.com");
        }
        return uid;
    }   
    this.getYYID = function(){
        var yyid = "";
        if(getCookie2("YYID") != null) {
            yyid = getCookie2("YYID");
        } else {
            yyid = "";  
        }
        return yyid;
    }   

    this.refurl = document.referrer == ""? "" : encodeURIComponent(document.referrer);

    //pl(page location)
    this.pl = encodeURIComponent(window.location.href);

    var u = this.getUid();

    this.getPv = function(){
        var t2 = new Date().getTime();
        var loadtime_onload = t2-t1;
        var loadtime_body = t3-t1;
        var pvImg = document.createElement('img');
        pvImg.src = pingServerUrl + "pv.GIF?t="+c+"&u="+ u +"&r="+this.refurl+"&pl="+this.pl+"&load="+loadtime_body+"&onloadtime="+loadtime_onload+"&solution="+solution;
    }
    this.getDlBtnClick = function(type, info,func){
        var n = new Date().getTime();
        var c = escape(n*1000+Math.round(Math.random()*1000));
        var parm = "";
        for(var key in info){
            parm+="&"+key+"="+info[key];
        }
        // var idx = info.idx;
        var ctImg = document.createElement('img');
        
        ctImg.src = pingServerUrl + "ct.GIF?t="+ c +"&u="+ u +"&pl="+ this.pl +"&type="+ type + parm + "&solution="+solution;
        ctImg.onerror = ctImg.onload = function(){
            func&&func();
        }

    };
}